import Vue from 'vue'
import store from '../store'
import IndexView from '@/views/index/IndexView.vue'
import VueRouter from 'vue-router'
import DetailView from "@/views/detail/DetailView.vue"
import SearchView from "@/views/search/SearchView.vue"
import DemoView from "@/views/DemoView.vue"
import DemoView2 from "@/views/DemoView2.vue"
import PersonalView from "@/views/personal/PersonalView.vue"
import ChartView from '@/views/chart/ChartView.vue'

import Vuex from 'vuex'
Vue.use(Vuex)

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'index',
    component: IndexView
  },
  {
    path: '/detail',
    name: 'detail',
    component: DetailView
  },
  {
    path: '/search',
    name: 'search',
    component: SearchView
  },
  {
    path: '/personal',
    name: 'personal',
    component: PersonalView
  },
  {
    path:'/chart',
    name: 'chart',
    component: ChartView
  },
  {
    path: '/demo',
    name: 'demo',
    component: DemoView
  },
  {
    path: '/demo2',
    name: 'demo2',
    component: DemoView2
  },

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  from
  // 判断目的地是否为个人中心
  if (to.path == '/personal') {
    // 若，登录，则允许
    if (store.state.userInfo && Object.keys(store.state.userInfo).length > 0 ) {
      next();
    }
    // 若未登录，则跳转到首页
    else {
      next({
        path: '/'
      })
    }
  }
  else {
    next();
  }


})


export default router
